package cn.mazexal.examapi.services.provider;

import cn.jiguang.sdk.api.PushApi;
import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.PushSendResult;
import cn.jiguang.sdk.bean.push.audience.Audience;
import cn.jiguang.sdk.bean.push.callback.Callback;
import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage;
import cn.jiguang.sdk.bean.push.options.Options;
import cn.jiguang.sdk.constants.ApiConstants;
import cn.jiguang.sdk.enums.platform.Platform;
import cn.mazexal.examapi.services.provider.push.PushMsg;
import cn.mazexal.examapi.services.provider.push.PushTarget;
import feign.Logger;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 极光推送服务
 *
 * @author: Mazexal
 * Date: 2025/7/7
 */
@Slf4j
@Component
public class JpushProvider {

    @Value("${jiguan.appKey}")
    private String appKey;

    @Value("${jiguan.appSecret}")
    private String appSecret;

    PushApi pushApi;

    @PostConstruct
    public void beforeInit() {
        pushApi = new PushApi.Builder()
                .setAppKey(appKey)
                .setMasterSecret(appSecret)
                .setLoggerLevel(Logger.Level.FULL)
                .build();
    }

    public void simplePush(PushMsg pushMsg) {
        PushSendParam param = new PushSendParam();
        // 通知内容
        NotificationMessage.Android android = new NotificationMessage.Android();
        android.setAlert(pushMsg.getBody());
        android.setTitle(pushMsg.getTitle());

        NotificationMessage.IOS iOS = new NotificationMessage.IOS();
        Map<String, String> iOSAlert = new HashMap<>();
        iOSAlert.put("title", "this is iOS title");
        iOS.setAlert(iOSAlert);
        iOS.setExtras(pushMsg.getParams());

        Map<String, Object> extrasMap = new HashMap<>();
        extrasMap.put("key1", "value1");
        extrasMap.put("key2", "value2");
        android.setExtras(extrasMap);
        iOS.setExtras(extrasMap);

        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.setAlert("this is alert");
        notificationMessage.setAndroid(android);
        notificationMessage.setIos(iOS);
        param.setNotification(notificationMessage);

        // 目标人群
        Audience audience = new Audience();
        audience.setRegistrationIdList(Arrays.asList(pushMsg.getPushTarget().getValue()));
        // 指定目标
         param.setAudience(audience);
        // 或者发送所有人
//        param.setAudience(ApiConstants.Audience.ALL);
        // 指定平台
        param.setPlatform(Arrays.asList(Platform.android, Platform.ios));
        // 或者发送所有平台
        // param.setPlatform(ApiConstants.Platform.ALL);

        // 短信补充
        // param.setSmsMessage();

        // 回调
        // param.setCallback();

        // options
        Options options = new Options();
        Map<String, Object> thirdPartyMap = new HashMap<>();
        Map<String, Object> huaweiMap = new HashMap<>();
        huaweiMap.put("distribution", "first_ospush");
        huaweiMap.put("importance", "NORMAL");
        huaweiMap.put("category", "MARKETING");
        thirdPartyMap.put("huawei", huaweiMap);
        options.setThirdPartyChannel(thirdPartyMap);
        param.setOptions(options);

        Map<String, Object> callbackParams = new HashMap<>();
        callbackParams.put("callbackKey", "callbackValue");
        Callback callback = new Callback();
        callback.setParams(callbackParams);
        param.setCallback(callback);
        log.info("param:{}", param);
        // 发送
        try {
            PushSendResult result = pushApi.send(param);
            log.info("result:{}", result);
        } catch (Exception e) {
            log.error("极光推送失败", e);
        }
    }

}
